home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / +ORC / Orc pac 2 / FILEZ.ZIP / DUMP.ZIP / DUMP.C next >
Encoding:
C/C++ Source or Header  |  1996-10-16  |  6.0 KB  |  243 lines

  1. /*
  2. **                HEX FILE DUMP UTILITY
  3. **   Displays any file in hex and character representation
  4. **   in color or monochrome, depending upon type of video
  5. **   card installed.  Displays 20 lines per screen and waits
  6. **   for a keypress.
  7. **
  8. **   Rev 1.00   8-09-87 for Turbo-C  S.E. Margison
  9. **
  10. **   As distributed, this program requires (for compilation):
  11. **     "Steve's Turbo-C Library" version 1.30 or later
  12. **   which may be obtained without registration from many Bulletin
  13. **   Board Systems including:
  14. **      Compuserve IBMSW
  15. **      Cul-De-Sac (Holliston, MA.)
  16. **      GEnie
  17. **   and software library houses including:
  18. **      Public (Software) Library (Houston, TX.)
  19. **
  20. **   or by registration:
  21. **      $10 for Docs, Small Model Library
  22. **      $25 for Docs, C, S, M, L, H libraries, and complete library source
  23. **              in C and Assembler
  24. **     Steven E. Margison
  25. **     124 Sixth Street
  26. **     Downers Grove, IL, 60515
  27. **
  28. **
  29. ** Program requires at least one parameter, the name of the file to dump.
  30. ** A second parameter is taken to be an offset value to start dumping,
  31. ** default is the start of the file.  If a third value is present it is
  32. ** taken to be a hex byte value to highlite whenever encountered in the file.
  33. **
  34. ** More than a utility, this is a good demonstration of the direct
  35. ** video subroutines and video page switching mechanism (for CGA cards).
  36. ** Look upon this as a tutorial as well as a handy utility.
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include <smdefs.h>
  42. #include <screen.h>
  43. #include <keys.h>
  44.  
  45. int adrclr,    /* address field color */
  46.     hexclr,    /* hex data color */
  47.     highclr,   /* highlite color */
  48.     prompt,    /* prompt line color */
  49.     ascclr;    /* ascii color */
  50.  
  51. char tbuf[82];
  52. int buffer[16];
  53. unsigned long offset = 0;
  54. FILE *f;
  55.  
  56. int mono, cpage, eoflag;
  57.  
  58. main(argc,argv)
  59. int argc;
  60. char *argv[];
  61. {
  62.    int i, lines, view, vflag;
  63.  
  64.    eoflag = vflag = NO;      /* default highlite off */
  65.  
  66.    if ((argc < 2) or (argc > 4))
  67.       error("Use: dump file [starting offset in hex] [highlite byte]");
  68.  
  69.    if((f = fopen(argv[1], "rb")) is NULL)
  70.       cant(argv[1]);
  71.  
  72.    if(argc > 2)
  73.    {
  74.       sscanf(argv[2], "%lx", &offset);
  75.       fseek(f, offset, 0);
  76.    }
  77.  
  78.    if (argc is 4)
  79.    {                                    /* get the value to highlite */
  80.       sscanf(argv[3], "%02x", &view);
  81.       vflag = YES;
  82.    }
  83.  
  84.    i = stuff(0);   /* check video type */
  85.  
  86.    if (i is MONO)
  87.    {
  88.       mono = TRUE;
  89.       prompt = BLINKING;
  90.       adrclr = WHITE;
  91.       hexclr = HIWHITE;
  92.       ascclr = WHITE;
  93.       highclr = HIGHBLINK;
  94.    }
  95.    else
  96.    {         /* for CGA cards */
  97.       vmode(CLR80);
  98.       mono = FALSE;
  99.       cpage = 1;       /* we'll write to page 1 first */
  100.       prompt = BROWN;
  101.       adrclr = LTGREEN;
  102.       hexclr = YELLOW;
  103.       ascclr = LTCYAN;
  104.       highclr = HIWHITE;
  105.    }
  106.  
  107.    dvid_init();    /* use direct video access routines */
  108.    dvid_cls();
  109.    dvid_move(0,0);
  110.    dvid_flush();
  111.  
  112.    if (!mono)
  113.    {
  114.       dvid_setpage(0, 1);       /* set page 0 as display page */
  115.       dvid_setpage(1, 0);       /* but set page 1 as writing page */
  116.    }
  117.  
  118.    cursor_style(5, 0, 0);    /* kill the cursor */
  119.  
  120.    lines = 20;
  121.  
  122.    for ever
  123.    {
  124.         /* if this were a level 1 file using read() and open()
  125.         ** the program would be faster.  Just a hint! */
  126.  
  127.       for (i = 0; i < 16; i++)
  128.          buffer[i] = fgetc(f);
  129.  
  130.       if(buffer[0] is -1)
  131.       {
  132.          if(lines is 20)
  133.             break;    /* we must've ended on even page */
  134.  
  135.          eoflag = TRUE;
  136.          goto alldone;
  137.       }
  138.  
  139.       dvid_attrib(adrclr);   /* set address color */
  140.       sprintf(tbuf, "%04lx: ",offset);  /* display address */
  141.       dvid_puts(tbuf);
  142.       dvid_attrib(hexclr);   /* set hex value color */
  143.  
  144.       for (i = 0; i < 16; i++)
  145.       {
  146.          if (buffer[i] isnot -1)
  147.          {
  148.             if(vflag and (buffer[i] is view))
  149.             {                                      /* highlite this byte? */
  150.                dvid_attrib(highclr);
  151.                sprintf(tbuf, "%02x ", buffer[i]);
  152.                dvid_puts(tbuf);
  153.                dvid_attrib(hexclr);
  154.             }
  155.             else
  156.             {
  157.                sprintf(tbuf, "%02x ", buffer[i]);
  158.                dvid_puts(tbuf);
  159.             }
  160.          }
  161.          else dvid_puts("   ");
  162.       }
  163.  
  164.       dvid_attrib(ascclr);  /* display ascii data color */
  165.       dvid_puts("   ");
  166.  
  167.       for (i = 0; i < 16; i++)
  168.       {
  169.          if (buffer[i] isnot -1)
  170.          {
  171.             if (!isprint(buffer[i]))
  172.                buffer[i] = '.';
  173.  
  174.             dvid_putchr(buffer[i]);
  175.          }
  176.          else
  177.             dvid_putchr(' ');
  178.       }
  179.  
  180.       dvid_putchr('\n');
  181.  
  182.       if(lines-- is 0)
  183.       {
  184.          alldone:
  185.  
  186.          dvid_attrib(prompt);
  187.  
  188.          dvid_say(22, 10, "Press any key to continue, ESCape to quit...");
  189.  
  190.          if(!mono)
  191.             dvid_setpage(cpage, 1);   /* display this page */
  192.  
  193.          if(getkey() is ESC)
  194.          {
  195.             dvid_setpage(0, 1);        /* return to page 0 */
  196.  
  197.             if(!mono)
  198.                cursor_style(1, 0, 0);  /* restore cursor */
  199.             else
  200.                cursor_style(2, 0, 0);
  201.  
  202.             aabort(1);
  203.          }
  204.  
  205.          if(eoflag)
  206.             break;
  207.  
  208.          cpage++;     /* bump page #, maintain 0-3 */
  209.          cpage &= 3;
  210.  
  211.          if(!mono)
  212.             dvid_setpage(cpage, 0);  /* set the next page for write */
  213.  
  214.          dvid_cls();
  215.          dvid_flush();
  216.          lines = 20;
  217.       }
  218.       offset += 16;
  219.    }                                        /* end of forever loop */
  220.  
  221.    dvid_setpage(0, 1);                      /* restore page 0 */
  222.    dvid_cls();
  223.    dvid_attrib(prompt);
  224.    dvid_say(22, 10, "End of File Encountered");
  225.  
  226.    if(!mono)
  227.       cursor_style(1, 0, 0);  /* restore cursor */
  228.    else
  229.       cursor_style(2, 0, 0);
  230.  
  231.    dvid_flush();
  232.    dvid_done();
  233. }
  234.  
  235.  
  236. dvid_puts(buf)
  237. char *buf;
  238. {
  239.    while(*buf isnot NULL)
  240.       dvid_putchr(*buf++);
  241. }
  242.  
  243.